<!--
 * @Author: 秦少卫
 * @Date: 2024-12-19 16:30:00
 * @LastEditors: 秦少卫
 * @LastEditTime: 2024-12-19 16:30:00
 * @Description: 登录页面
-->

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>Vue Fabric Editor</h1>
        <p>请登录以继续使用</p>
      </div>

      <Form ref="loginForm" :model="formData" :rules="rules" class="login-form">
        <FormItem prop="identifier">
          <Input
            v-model="formData.identifier"
            size="large"
            placeholder="请输入用户名"
            prefix="ios-person"
          />
        </FormItem>

        <FormItem prop="password">
          <Input
            v-model="formData.password"
            type="password"
            size="large"
            placeholder="请输入密码"
            prefix="ios-lock"
            @on-enter="handleLogin"
          />
        </FormItem>

        <FormItem>
          <Button type="primary" size="large" long :loading="loading" @click="handleLogin">
            登录
          </Button>
        </FormItem>
      </Form>

      <div class="login-tips">
        <p>默认账号：admin</p>
        <p>默认密码：admin123</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { Message } from 'view-ui-plus';
import { login, setToken } from '@/api/user';

const router = useRouter();
const loading = ref(false);

// 表单数据
const formData = reactive({
  identifier: '',
  password: '',
});

// 表单验证规则
const rules = reactive({
  identifier: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
  ],
});

const loginForm = ref(null);

// 登录处理
const handleLogin = () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      loading.value = true;

      login(formData)
        .then((res) => {
          if (res.data && res.data.jwt) {
            // 设置 JWT token
            setToken(res.data.jwt);
            // 设置用户信息
            localStorage.setItem('userInfo', JSON.stringify(res.data.user));

            Message.success('登录成功！');
            // 跳转到首页
            router.push('/');
          } else {
            Message.error('登录响应数据格式错误');
          }
        })
        .catch((error) => {
          console.error('登录错误:', error);
          Message.error(error.response?.data?.error?.message || '登录失败！');
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
};
</script>

<style scoped lang="less">
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;

  .login-header {
    text-align: center;
    margin-bottom: 30px;

    h1 {
      color: #333;
      font-size: 28px;
      font-weight: 600;
      margin-bottom: 8px;
    }

    p {
      color: #666;
      font-size: 14px;
      margin: 0;
    }
  }

  .login-form {
    .ivu-form-item {
      margin-bottom: 20px;
    }

    .ivu-input-wrapper {
      border-radius: 8px;
    }

    .ivu-btn {
      border-radius: 8px;
      height: 44px;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .login-tips {
    margin-top: 20px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;
    border-left: 4px solid #667eea;

    p {
      margin: 0;
      font-size: 13px;
      color: #666;
      line-height: 1.5;
    }
  }
}

@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;

    .login-header h1 {
      font-size: 24px;
    }
  }
}
</style>
